package cn.szabadsag.org.controller;

import cn.szabadsag.org.domain.DicDetail;
import cn.szabadsag.org.query.DicTypeQuery;
import cn.szabadsag.org.service.IDicDetailService;
import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典明细接口
 *
 * @author yangcong
 * @date 2022年03月07日 9:26
 * @since v1.0
 */
@RestController
@RequestMapping("/dicDetail")
@Api(tags = "dicDetail维护的接口")
public class DicDetailController {

    @Autowired
    private IDicDetailService dicDetailService;


    /**
     * 新增或修改
     */
    @PutMapping
    public AjaxResult saveOrUpt(@RequestBody DicDetail dicDetail){
        try {
            if (dicDetail.getId() == null){
                //  新增
                dicDetailService.save(dicDetail);
            }else {
                //  修改
                dicDetailService.update(dicDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            dicDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            dicDetailService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 查询单个
     */
    @GetMapping(value = "/{id}")
    public DicDetail findById(@PathVariable("id") Long id){
        return dicDetailService.loadById(id);
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询数据字典的方法")
    public List<DicDetail> loadAll(){
        return dicDetailService.loadAll();
    }

    /**
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public QueryResult<DicDetail> queryPage(@RequestBody DicTypeQuery query){
        return  dicDetailService.queryPage(query);
    }

}
